﻿package fj.ui
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import fj.events.FJMenuEvent;
	
	public class FJMenu extends MovieClip
	{
		public var autoSelect:Boolean = true;
		
		private var _arrayBtns:Array;
		private var _selected:String;
		
		public function set arrayBtns( a:Array ):void
		{
			_arrayBtns = a;
			for( var i:uint; i < _arrayBtns.length; i++ ){
				var btn:MovieClip = _arrayBtns[i] as MovieClip;
				btn.addEventListener( MouseEvent.CLICK, menuClick )
			}
		}
		public function set selected ( s:String ):void
		{
			_selected = s;
			for( var i:uint = 0; i < _arrayBtns.length; i++ ){
				var btn:MovieClip = _arrayBtns[i] as MovieClip;
				if(btn.selected){
					btn.selected = false;
				}
			}
			var btnSelected:MovieClip = getChildByName(s) as MovieClip;
			btnSelected.selected = true;
		}
		public function get selected ():String
		{
			return _selected;
		}
		protected function menuClick( e:MouseEvent ):void
		{
			if( autoSelect ){
				selected = e.currentTarget.name;
			}
			var evento:FJMenuEvent = new FJMenuEvent( e.currentTarget as MovieClip );
			dispatchEvent( evento );
		}
	}
}